<?php

class CF_Controller_Action extends Zend_Controller_Action
{


	public function preDispatch(){

		$acl = Zend_Registry::get('acl');
		//$this->view->acl = $acl;
		$auth = Zend_Auth::getInstance();
		//var_dump($auth->getIdentity());
		//&& isset($auth->getStorage()->read()->role)
		$controller = $this->getRequest()->getControllerName();
		$action = $this->getRequest()->getActionName();
		//if we in 'auth/index' we just do default things in controllers

		if($controller == 'auth' && $action == 'index'){
			return false;
		}
		if (true === $auth->hasIdentity())
		{
			$controller = $this->getRequest()->getControllerName();
			$action = $this->getRequest()->getActionName();
			$role_id = $auth->getStorage()->read()->role_id;
			$roleDb = new Project_Role_Table();
			$role = $roleDb->getRoles($role_id)->role;
			if($acl->isAllowed($role, $controller, $action))
			{
			}
			else
			{
				return $this->_redirect('error/denied/role/'.urlencode($role));
			}
		}
		else //if($controller != 'auth' && $action != 'index')
		{
			$this->_redirect('auth/index/');

		}

	}
}

//echo '<pre>';
//var_dump(Zend_Auth::getInstance()->hasIdentity());
//var_dump(Zend_Controller_Front::getInstance()->getRequest()->getControllerName());
//var_dump(Zend_Controller_Front::getInstance()->getRequest()->getActionName());
//echo '</pre>';